//
//  TYCRefreshView.swift
//  RefreshContrlAndKVO
//
//  Created by 中发 on 2018/6/19.
//  Copyright © 2018年 中发. All rights reserved.
//

import UIKit

/// 刷新视图
class TYCRefreshView: UIView {

    ///刷新状态
    /**
     iOS中系统中UIView封装的旋转动画
     - 默认逆时针旋转
     - 就近原则
     - 要想实现同方向旋转，需要调整一个非常小的数字
     - 如果想实现360度旋转，需要核心动画CABaseAnimation
     */
    var refreshState: TYCRefreshStatus = .Normal {
        didSet {
            switch refreshState {
            case .Normal:
                //恢复状态
                tipIcon?.isHidden = false
                indicator?.stopAnimating()
                tipLabel?.text = "继续使劲拉.."
                UIView.animate(withDuration: 0.25, animations: {
                    self.tipIcon?.transform = CGAffineTransform.identity
                })
            case .Pulling:
                tipLabel?.text = "放手就刷新.."
                UIView.animate(withDuration: 0.25, animations: {
                    self.tipIcon?.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi - 0.001))
                })
            case .WillRefresh:
                tipLabel?.text = "正在刷新中.."
                //隐藏提示图标
                tipIcon?.isHidden = true
                //显示菊花
                indicator?.startAnimating()
            }
        }
    }
    ///父视图的高度--为了刷新控件不需要关心当前具体的刷新视图是谁
    var parentViewHeight: CGFloat = 0
    
    /// 指示器
    @IBOutlet weak var indicator: UIActivityIndicatorView?
    ///指示视图
    @IBOutlet weak var tipIcon: UIImageView?
    /// 指示标签
    @IBOutlet weak var tipLabel: UILabel?
    
    class func refreshView() -> TYCRefreshView {
        let nib = UINib(nibName: "TYCRefreshView", bundle: nil)
        return nib.instantiate(withOwner: nil, options: nil)[0] as! TYCRefreshView
    }
}
